package com.wanxi.dao;

import com.wanxi.model.BrandModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class BrandDao {
    private static List<BrandModel> list = new ArrayList<>();

    public int add(BrandModel model) {
//        1.用随机数：
//        2.用毫秒数
//        3.用UUID
//        4.用int  只不过id自动自增
//        拿到list的最后一条数据，id=1即可
        int size = list.size();
        int id = 1;
        if (size != 0) {
            BrandModel model1 = list.get(size - 1);
            id = model1.getId() + 1;
        }
        model.setId(id);
        list.add(model);
        return 1;
    }

    public List<BrandModel> findAll() {
        return list;
    }

    public int delete(BrandModel model) {
//        list.remove(model);
        Iterator<BrandModel> iterator = list.iterator();
        while (iterator.hasNext()) {
            BrandModel brandModel = iterator.next();
            if (brandModel.getId().equals(model.getId())) {
                iterator.remove();
            }
        }
        return 0;
    }

    public static void main(String[] args) {
//        long l = System.currentTimeMillis();
////        1682 4879 8442 0
//        System.out.println(l);
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid.toString().replace("-", ""));

    }

    public BrandModel findById(BrandModel brandModel) {
        for (int i = 0; i < list.size(); i++) {
            if (brandModel.getId().equals(list.get(i).getId())) {
                return list.get(i);
            }
        }
        return null;
    }

    public int update(BrandModel model) {
        for (int i = 0; i < list.size(); i++) {
            if (model.getId().equals(list.get(i).getId())) {
                list.set(i, model);
                return 0;
            }
        }
        return 0;
    }
}
